<?php
class checkAction extends Action {
    function index(){
        $uid=session("uid");
        $username=session("username");
        if(!$uid){
            exit('请先登陆');
        }
        $this->assign("uid",$uid);
        $this->assign("username",$username);
        $this->display();
    }
    function check(){
        if(!function_exists("curl_init")){
            exit('出错，不支持curl');
        }
        //检测类型 1:today 2:yestoday 3:week 
        $ctype=$this->_param("ctype")?$this->_param("ctype"):"20";
        $uid=$this->_param("uid")?$this->_param("uid"):"0";
        $where=array();
        if($uid){
            $where['uid']=$uid;
        }
        $todaytimestamp=strtotime(date("Y-m-d",  time()));//今天0点的时间戳
        $yestodaytimestamp=strtotime(date("Y-m-d",  time()))-86400;//昨天的时间戳
        $weektimestamp=strtotime(date("Y-m-d",  time()))-86400*6;//6天前的时间戳
        $timestamps=array($todaytimestamp,$yestodaytimestamp,$weektimestamp);
        $where['pubdate']=array("gt",$timestamps[($ctype-1)]);
        $links=M('links');
        $startdd=$this->_param('startdd')?$this->_param('startdd'):0;//开始行
        $pagesize=$this->_param('pagesize')?$this->_param('pagesize'):1;//每次处理
        $total=$links->field("linkid")->where($where)->count();//总数量
        if($startdd > $total){
            exit("检测完成,<a href=# onclick='parent.window.location.reload();'>确定</a>");
            
        }
        $linksarr=$links->field("uid",true)->where($where)->limit("{$startdd},{$pagesize}")->select();//分批读取链接数组
        //循环处理
        foreach($linksarr as $v){
            $ch1=curl_init();
            $ch2=curl_init();
            curl_setopt($ch1,CURLOPT_URL,$v['link']);//外链地址句柄
            curl_setopt($ch2,CURLOPT_URL,"http://www.baidu.com/s?wd=".urlencode($v['link']));
            curl_setopt($ch1,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch2,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch1,CURLOPT_TIMEOUT,10);
            curl_setopt($ch2,CURLOPT_TIMEOUT,10);
            curl_setopt($ch1,CURLOPT_HTTPHEADER,array("User-Agent:Baiduspider+(+http://www.baidu.com/search/spider.htm)"));
            curl_setopt($ch2,CURLOPT_HTTPHEADER,array("User-Agent:Baiduspider+(+http://www.baidu.com/search/spider.htm)"));
            $content1=curl_exec($ch1);//外链地址源码
            
            curl_close($ch1);
            $content2=curl_exec($ch2);//百度收录测试源码
            curl_close($ch2);
            
            if($content1!==FALSE){
                if(strpos($content1,$v['word'])===FALSE){
                    if($v['pubstatus']!="0"){
                        $links->where(array("linkid"=>$v['linkid']))->data(array("pubstatus"=>"0"))->save();
                    }
                }else{
                    if($v['pubstatus']!="1"){
                        $links->where(array("linkid"=>$v['linkid']))->data(array("pubstatus"=>"1"))->save();
                    }
                }
            }
            if($content2!==FALSE){
                $linkparse=parse_url($v['link']);
                
                if(strpos($content2,$linkparse['host'])!==FALSE){
                    if($v['bdstatus']!="1"){
                        $links->where(array("linkid"=>$v['linkid']))->data(array("bdstatus"=>"1"))->save();
                    }
                }else{
                    if($v['bdstatus']!="0"){
                        $links->where(array("linkid"=>$v['linkid']))->data(array("bdstatus"=>"2"))->save();
                    }
                }
            }
            $lastchecktime=time();
            $links->where(array("linkid"=>$v['linkid']))->data(array("lastcheck"=>$lastchecktime))->save();
        }
        //显示进度消息
        $jindu="当前进度：$startdd / $total";
        $enddd=$startdd+$pagesize;
        $this->assign("waitSecond","0");
        $this->success($jindu, U("check/check?total={$total}&startdd=$enddd&pagesize=$pagesize"));
        exit;
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,"http://www.01junshi.com");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $html=curl_exec($ch);
        curl_close($ch);
    }
    
    
}
